<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Requests\CateInsertRequest;  //引入分类输入验证的文件 ,注意命名空间
use DB;  //引入DB类文件

use App\Http\Requests;
use App\Http\Controllers\Controller;

class CateController extends Controller
{
   // 显示分类列表
    public function getIndex()
    {
        $cates = DB::select("select *,concat(path,',',cid) as paths from cates order by paths");
        foreach ($cates as $k => $v){
            $arr = explode(',',$v->path);
            $len = count($arr)-1;
            $cates[$k]->cname = str_repeat('----->',$len).$v->cname;
        }

        // dd($cates);

       return view('admin.cates.index',['cates'=>$cates]);
    }

    // 封装一个方法用来处理分类图片上传
    private function upload($request,$file)
    {
        if($request->hasFile($file)){
            $suffix = $request->file($file)->getClientOriginalExtension(); // 获取文件后缀名
            $name = md5(time()).rand(0,99999);  //生成一个随机文件名
            $request->file($file)->move('./uploads/catesimg/',$name.'.'.$suffix);  //移动
            return '/uploads/catesimg/'.$name.'.'.$suffix;  //返回路径信息
        }
    }

    // 添加分类
    public function getAdd()
    {
    	$cates = DB::select("select *,concat(path,',',cid) as paths from cates order by paths");

        foreach ($cates as $k => $v){
            $arr = explode(',',$v->path);
            $len = count($arr)-1;
            $cates[$k]->cname = str_repeat('----->',$len).$v->cname;
        }

    	// dd($cates);
    	return view('admin.cates.add',['cates'=>$cates]);
    }

    // 处理添加分类信息
    public function postInsert(CateInsertRequest $request)
    {

        $data = $request->except('_token');

        $data['cpic'] = $this->upload($request,'cpic');  //调用函数处理图片

        $data['pname'] = '';
    	// 判断当前添加的类的父级是否为根
    	if($data['pid'] == 0){
    		$data['path'] = 0;
            $data['pname'] = '根类';
    	}else{
    		$p_path = DB::table('cates')->where('cid',$data['pid'])->first();  //查询父级path
    		$data['path']= $p_path->path.','.$data['pid'];  //拼接path
            $data['pname'] = DB::select('select cname from cates where cid = '.$data['pid'])[0]->cname;
    	}

    	$res = DB::table('cates')->insert($data);
    	if($res){
    		return redirect('/admin/cate/index')->with('success','添加分类成功');
    	}else{
    		return back()->with('error','添加分类失败');
    	}
    }

    // 删除
    public function postDel(Request $request)
    {
        $cid = $request->input('cid');  //获取分类id

        // 判断是否有子分类
        $res1 = DB::select('select * from cates where pid='.$cid);
        if($res1){
            echo 1;return;
        }

        // 判断该分类下是否有商品
        $res2 = DB::select('select * from goods where cid='.$cid);
        if($res2){
            echo 2;return;
        }

        // 执行删除
        $res = DB::table('cates')->where('cid',$cid)->delete();  //删除用户
        if($res){
            echo 3; //返回  ajax判断
        }
    }

    // 修改分类
    public function getEdit(Request $request)
    {
        // 查询所有分类信息
        $cates = DB::select("select *,concat(path,',',cid) as paths from cates order by paths");

        foreach ($cates as $k => $v){
            $arr = explode(',',$v->path);
            $len = count($arr)-1;
            $cates[$k]->cname = str_repeat('----->',$len).$v->cname;
        }

        // 查询需要修改的分类信息
        $cid = $request->id;  //获取id
        $cate = DB::table('cates')->where('cid',$cid)->first(); 
        
        return view('admin.cates.edit',['cates'=>$cates,'cate'=>$cate]);
    }

    // 处理修改分类信息
    public function postDoedit(Request $request)
    {
        $data = $request->except('_token','oldpic');

        // 判断是否修改图片
        if(empty($request->spic)){
            $data['cpic'] = $request->oldpic;
        }else{
            $data['cpic'] = $this->upload($request,'cpic');  //调用函数处理图片
        }

        // dd($data);
        $res = DB::table('cates')->where('cid',$request->cid)->update($data);

        if($res){
            return redirect('/admin/cate/index')->with('success','修改分类成功');
        }else{
            return back()->with('error','修改分类失败');
        }
    }
}
